<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: MovePortlet.php 367 2010-02-06 20:04:08Z steen.rabol@gmail.com $
 *
 */

class MovePortlet extends PortalSystemPage
{
	private $_pagename		= '';
	private $_controlname	= '';
	private $_portletname	= '';
	private $_recid			= 0;

	public function onInit($param)
	{
		parent::onInit($param);

		if(!$this->User)
		{
			$this->reportError(1,Prado::localize("You need to be logged in to use this page"));
		}

		if($this->User->RoleId != $this->Application->Parameters['AdminRole'])
		{
			$this->reportError(1,Prado::localize("You do not have accress right to access this page or featue"));
		}
	}

	public function onLoad($param)
	{
		if(($la = $this->Request['la']) == null)
		{
			$this->reportError(1,"Move portlet called with wrong number of parameters");
		}

		$layoutares = array();
		$layoutares[] = array("id" => "0","name" => Prado::localize("Select area"));

		foreach(explode(";",$la) as $l)
		{
			$layoutares[] = array("id" => $l, "name" => $l);
		}

		$this->Title = $this->Application->Parameters['SiteTitle'] . " - " . Prado::localize("Element properties");
		if(!$this->IsPostBack)
		{
			if(($pid=$this->Request['pid'])!==null)
			{
				$this->pid->Value	= $pid;
				$this->_pagename	= $pid;
			}

			if(($cid=$this->Request['cid'])!==null)
			{
				$this->cid->Value	= $cid;
				$this->_controlname	= $cid;
			}

			if(($poid=$this->Request['poid'])!==null)
			{
				$this->rid->Value	= $poid;
				$this->_recid		= $poid;
			}

			if(($pname = $this->Request['pname'])!==null)
			{
				$this->_portletname	= $pname;
			}

			$this->MoveTo->DataSource	= $layoutares;
			$this->MoveTo->dataBind();

			$row = $this->DataAccess->createCommand("select * from tblpagecontent where id=$poid")->queryRow();
			$this->CancelLink->NavigateUrl		= $this->Service->constructUrl($this->pid->Value,array("PageMode" => $this->Page->EditMode));
		}
	}

	public function MoveElementButtonClicked($sender,$param)
	{
		$rid			= $this->rid->Value;
		$cid			= $this->cid->Value;
		$pid			= $this->pid->Value;
		$moveto			= $this->MoveTo->SelectedValue;
		$nextorder		= $this->DataAccess->createCommand("select count(*) from tblpagecontent where pagename='$pid' && controlname='$moveto'")->queryScalar();
		$nextorder++;

		$sql			= "update tblpagecontent set controlname='$moveto',displayorder='$nextorder' where id=$rid";
		$this->DataAccess->createCommand($sql)->execute();
		$this->gotoPage($this->pid->Value,array("PageMode" => $this->Page->EditMode));
	}

	public function PageName()
	{
		return $this->_pagename;
	}

	public function ControlName()
	{
		return $this->_controlname;
	}

	public function ElementName()
	{
		return $this->_portletname;
	}
}
?>